#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>

//Observe whether the control Douyin program is carried out
//观察控制抖音程序是否进行
static bool flag = true;
void handler(int sig)
{
	flag = false;
}

int CheckDouyin()
{
	FILE *f;
	char buffer[128]={'\0'};
	f = popen("ps -elf | grep UartTest | grep -v grep","r");
	fgets(buffer,128,f);

	if(strstr(buffer,"UartTest")!=NULL){
		return 0;
	}
	else{
		return -1;
	}

}

int main()
{
	time_t t;
	int fd;
	//创建守护进程
	if(-1 == daemon(0, 0))
	{
		printf("daemon error\n");
		exit(1);
	}
	//设置信号处理函数
	struct sigaction act;
	act.sa_handler = handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	if(sigaction(SIGQUIT, &act, NULL))
	{
		printf("sigaction error.\n");
		exit(0);
	}
	//进程工作内容
	while(flag)
	{
		if(CheckDouyin() == -1)
		{
			system("/home/orangepi/MyProject/douyin/UartTest /dev/ttyS5 &");
		}
		sleep(2);
	}
	return 0;
}
